package DataBaseUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

import peng.yang.express.Bean.ExpressMember;

/**
 * Created by Administrator on 2016/6/28.
 */
public class SQLLiteHelperCheck extends SQLiteOpenHelper{

    private static final String DB_NAME = "ExpressCheck";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME = "expressCheck";
    private static final String COL_num = "num";
    private static final String COL_expressName = "expressName";
    private static final String COL_state = "state";
    private static final String COL_goodName = "goodName";
    private static final String COL_fileImageUrl = "fileImageUrl";

    String sql = "CREATE TABLE " + TABLE_NAME
            + "(num VARCHAR(20),"
            + " expressName VARCHAR(20),"
            + " goodName VARCHAR(20),"
            + " fileImageUrl VARCHAR(20),"
            + " state VARCHAR(20))";

    //构造函数，创建数据库
    public SQLLiteHelperCheck(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

    }
    //建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);

    }
//更新数据库的时候调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }

/*
//    获得数据库所有的数据
    public List<ExpressMember> getAllMembers(){
        List<ExpressMember> memberList = new ArrayList<>();
        SQLiteDatabase database = getReadableDatabase();
//        要查询的字段
        String [] columns = {COL_num,COL_expressName,COL_ImageId };
//        调用query查询
        Cursor cursor = database.query(TABLE_NAME, columns, null, null, null, null, null);
        while (cursor.moveToNext()){
            String num = cursor.getString(cursor.getColumnIndex(COL_num));
            String expressName = cursor.getString(cursor.getColumnIndex(COL_expressName));
            int ImageId = cursor.getInt(cursor.getColumnIndex(COL_ImageId ));

            ExpressMember member = new ExpressMember();
            member.setNum(num);
            member.setExpressname(expressName);
            member.setImageId(ImageId);

            memberList.add(member);
        }
        return memberList;
    }

//    根据条件获得数据库信息

    public ExpressMember findById(String COL_num) {
        SQLiteDatabase db = getWritableDatabase();
        String [] columns = {COL_num,COL_expressName,COL_ImageId };
        String selection = COL_num + " = ?;";
        String[] selectionArgs = {COL_num};
        Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs,
                null, null, null);
        ExpressMember member = null;
        if (cursor.moveToNext()) {
            String num = cursor.getString(cursor.getColumnIndex(COL_num));
            String expressName = cursor.getString(cursor.getColumnIndex(COL_expressName));
            int ImageId = cursor.getInt(cursor.getColumnIndex(COL_ImageId ));
            member = new ExpressMember();
            member.setNum(num);
            member.setExpressname(expressName);
            member.setImageId(ImageId);
        }
        cursor.close();
        return member;
    }

    //插入一条记录
    public long insert(ExpressMember member) {
        SQLiteDatabase database = getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("COL_num",member.getNum());
        cv.put("COL_expressName", member.getExpressname());
        cv.put("COL_ImageId", member.getImageId());
        cv.put("COL_goodName", member.getGoodname());

        long row = database.insert(TABLE_NAME, null, cv);
        return row;
    }

    */
/**
     *
     * @param member
     * @return
     *//*

    public int update(ExpressMember member) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("COL_num",member.getNum());
        cv.put("COL_expressName", member.getExpressname());
        cv.put("COL_ImageId", member.getImageId());
        cv.put("COL_goodName", member.getGoodname());
        String whereClause = COL_num + " = ?;";
        String[] whereArgs = {member.getNum()};
        return db.update(TABLE_NAME, cv, whereClause, whereArgs);
    }


    public int deleteById(String COL_num) {
        SQLiteDatabase db = getWritableDatabase();
        String whereClause = COL_num + " = ?;";
//        字符串变为整形数为String.valueOf(id)
        String[] whereArgs = {COL_num};
        return db.delete(TABLE_NAME, whereClause, whereArgs);
    }
*/

}
